package com.css.fxfzcpxx.modules.customizedServices.repository.impl;

import com.css.fxfzcpxx.constants.YNEnum;
import com.css.fxfzcpxx.modules.customizedServices.entity.CusServices;
import com.css.fxfzcpxx.modules.customizedServices.repository.CusServicesRepository;
import com.css.fxfzcpxx.modules.thematicMap.entity.ThematicMap;
import com.css.fxfzcpxx.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2023/4/14 9:54
 * @Description:
 */
@Repository
public class CusServicesRepositoryimpl implements CusServicesRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void save(CusServices cusServices) {
        String sql = "insert into cpxx_customized_services(id, service_type, service_purpose, service_description, applicant, telephone, create_user, create_time, message_status) " +
                "values(:id, :serviceType, :servicePurpose, :serviceDescription, :applicant, :telephone, :createUser, :createTime, :messageStatus)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(cusServices);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public Map<String, Object> getPage(String keyword, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from cpxx_customized_services");
        StringBuilder whereSql = new StringBuilder(" where 1=1");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        if (!PlatformObjectUtils.isEmpty(keyword)){
            whereSql.append(" and (service_type like '%"+keyword+"%' or service_purpose like '%"+keyword+"%' " +
                    "or service_description like '%"+keyword+"%' " +
                    "or applicant like '%"+keyword+"%' " +
                    "or telephone like '%"+keyword+"%' " +
                    "or create_user like '%"+keyword+"%')");
        }
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CusServices.class)).size();
        List<CusServices> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(CusServices.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public CusServices findById(String id) {
        String sql = "select * from  cpxx_customized_services where id = ? ";
        CusServices cusServices = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(CusServices.class), id);
        return cusServices;
    }

    @Override
    public void updateMessageStatus(String id, String messageStatus) {
        String sql = "update cpxx_customized_services set message_status = ?  where id = ?";
        jdbcTemplate.update(sql, messageStatus, id);
    }
}
